package com.yonyou.cyxdms.sysAuth.service.service.favorite.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yonyou.cyx.framework.util.bean.ApplicationContextHelper;
import com.yonyou.cyxdms.framework.bean.dto.user_info.LoginInfoDto;
import com.yonyou.cyxdms.sysAuth.service.entity.MenuFavoritePO;
import com.yonyou.cyxdms.sysAuth.service.mapper.MenuFavoriteMapper;
import com.yonyou.cyxdms.sysAuth.service.service.favorite.MenuFavoriteService;
import com.yonyou.cyxdms.sysAuth.service.vo.favorite.FavoriteVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author maxingjian
 * @since 2019-11-07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class MenuFavoriteServiceImpl extends ServiceImpl<MenuFavoriteMapper, MenuFavoritePO> implements MenuFavoriteService {
    //日志对象
    private static final Logger logger = LoggerFactory.getLogger(MenuFavoriteServiceImpl.class);

    @Autowired
    MenuFavoriteMapper menuFavoriteMapper;
    @Override
    public FavoriteVO saveFavorite(FavoriteVO favoriteVO) {
        LoginInfoDto loginInfoDto = ApplicationContextHelper.getBeanByType(LoginInfoDto.class);
        QueryWrapper<MenuFavoritePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MenuFavoritePO::getUserOrgId,loginInfoDto.getUserOrgId()).eq(MenuFavoritePO::getMenuId,favoriteVO.getMenuId());
        List<MenuFavoritePO> list = menuFavoriteMapper.selectList(queryWrapper);
        if(list.size()>0){
            MenuFavoritePO menufavoritepo = list.get(0);
            menufavoritepo.setFavorite(favoriteVO.getIsFavorite());
            menuFavoriteMapper.updateById(menufavoritepo);
        }else
        {
            MenuFavoritePO menufavoritepo =new MenuFavoritePO();
            menufavoritepo.setCompanyId(loginInfoDto.getCompanyId());
            menufavoritepo.setMenuId(Long.parseLong(favoriteVO.getMenuId()));
            menufavoritepo.setFavorite(favoriteVO.getIsFavorite());
            menufavoritepo.setUserCode(loginInfoDto.getUserCode());
            menufavoritepo.setUserOrgId(loginInfoDto.getUserOrgId());
            menufavoritepo.setAppId(loginInfoDto.getAppId());
            menuFavoriteMapper.insert(menufavoritepo);
        }
        return null;
    }
}
